package AppliactionOfStack;

import java.util.Enumeration;
import java.util.Scanner;
import java.util.Stack;



// 利用中缀表达式求值 3.2.5
public class CalculateExpression_mid {

    static char[][] pre={
            {'>','>','<','<','<','>','>'},
            {'>','>','<','<','<','>','>'},
            {'>','>','>','>','<','>','>'},
            {'>','>','>','>','<','>','>'},
            {'<','<','<','<','<','=','0'},  // 此行"(" = ")"表示左右括号相遇，括号内运算已完成
            {'>','>','>','>','0','>','>'},
            {'<','<','<','<','<','0','='}   //“#" = "#” 表示整个表达式求值完毕
            };                              //  "0"表示不可能出现这种情况 ( 语法错误 )

    public static char Precede(char a,char b){
        int i=-1;
        int j=-1;
        switch(a){
            case'+':i=0;break;
            case'-':i=1;break;
            case'*':i=2;break;
            case'/':i=3;break;
            case'(':i=4;break;
            case')':i=5;break;
            case'#':i=6;break;   // # 是表达式的结束符
        }
        switch(b){
            case'+':j=0;break;
            case'-':j=1;break;
            case'*':j=2;break;
            case'/':j=3;break;
            case'(':j=4;break;
            case')':j=5;break;
            case'#':j=6;break;
        }
        return pre[i][j];
    }

    public static void Show(Stack s){
        Enumeration elements = s.elements();
        while (elements.hasMoreElements()){
            System.out.println(elements.nextElement());
        }
    }

    public static int Operate(int m,int n,char x) {
        if (x == '+')
            return m + n;
        else if (x == '-')
            return m - n;
        else if (x == '*')
            return m * n;
        else if (x == '/')
            return m / n;
        return 0;
    }

    //表达式求值 3.2.5     中缀表达式
    public static void main(String[] args) {
        Stack<Integer> opnd=new Stack<Integer>();
        Stack<Character> optr=new Stack<>();
        optr.push('#');
        Scanner s=new Scanner(System.in);
        String ss=s.next();
        System.out.println(ss);
        char[] chars = ss.toCharArray();
        int i=0;
        char c=chars[i];
        //System.out.println(chars[0]);
        //System.out.println(chars.length);
        while (c!='#'||optr.peek()!='#'){
            if (Character.isDigit(c)){
                int temp=Integer.parseInt(c+"");
                //System.out.println(temp);
                opnd.push(temp);
                c=chars[++i];
            }else {
               switch (CalculateExpression_mid.Precede(optr.peek(),c)){
                   case '<':
                       optr.push(c);
                       c=chars[++i];
                       break;
                   case '=':
                       optr.pop();
                       c=chars[++i];
                       break;
                   case '>':
                       int b = opnd.pop();
                       int a =  opnd.pop();
                       char c1=optr.peek();
                       optr.pop();
                       int fin = CalculateExpression_mid.Operate(a, b, c1);
                       opnd.push(fin);
                       break;
               }
            }
        }
        System.out.println(opnd.peek());

    }
}
